﻿//Copyright 2008 Kristian Gundry And Rune Juhl-Petersen
//This file is part of Plain.

//Plain is free software: you can redistribute it and/or modify
//it under the terms of the GNU Lesser General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//any later version.

//Plain is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public License
//along with Plain.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Plain.Configuration;
using System.Windows.Markup;
using System.IO;
using System.Configuration;
using System.Web;
using log4net;

namespace Plain{
    /// <summary>
    /// static class for retrieving plain configuration
    /// </summary>
    public class PlainApplication{
        private static ILog log = LogManager.GetLogger("Plain.Domain.PlainApplication");
        private static PlainConfig plainConfig;
        private static object objLock = new object();
        /// <summary>
        /// Gets the <see cref="PlainConfig"/> object of the application
        /// </summary>
        public static PlainConfig Config { 
            get {
                if (plainConfig == null){
                    log.Debug("Loading Configuration");
                    lock (objLock){
                        FileStream stream = null;
                        try {
                            log.InfoFormat("Loading configuration with absolute path: {0}", ConfigurationManager.AppSettings["PlainApplicationConfiguration"]);
                            stream = new FileStream(ConfigurationManager.AppSettings["PlainApplicationConfiguration"], FileMode.Open);
                        } catch {
                            log.Info("Configuration load failed");
                        }
                        if (stream == null) {
                            try {
                                log.InfoFormat("Loading configuration with server path: {0}", ConfigurationManager.AppSettings["PlainApplicationConfiguration"]);
                                stream = new FileStream(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["PlainApplicationConfiguration"]), FileMode.Open);
                            } catch {
                                log.Info("Configuration load failed");
                            } 
                        }
                            
                            XamlReader reader = new XamlReader();
                        
                        plainConfig = (PlainConfig)XamlReader.Load(stream);
                    }
                }
                return plainConfig;
            }
            set { plainConfig = value; }
        }
    }
}
